package com.tadpole.music.view.activity.me;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.TabAdapter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.event.StudentRefreshEvent;
import com.tadpole.music.view.fragment.me.StudentMoNiFragment;
import com.tadpole.music.view.fragment.me.StudentShiZhanFragment;

import com.tadpole.music.view.widget.NoScrollViewPager;

import com.tadpole.music.view.widget.StudentScreenDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 做题详情
 * Created By 孙岩
 * on 2020/9/30
 */
public class QuestionDetailActivity extends BaseActivity {
    //回退
    private View view_back_icon;
    //筛选
    private View viewScreen;
    //TabLayout
    private TabLayout tabLayout;
    //NoScrollViewPager
    private NoScrollViewPager viewPager;
    private TabAdapter tabAdapter;
    //tab集合
    private List<String> tabList = new ArrayList<>();
    //viewpager绑定的fragment的集合
    private List<Fragment> fragmentList = new ArrayList<>();
    //学生id
    private String student_id;
    //学生头像
    private String student_avatar;
    //学生昵称
    private String student_nickname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_detail);
        student_id = getIntent().getStringExtra("student_id");
        student_avatar = getIntent().getStringExtra("student_avatar");
        student_nickname = getIntent().getStringExtra("student_nickname");
        initViews();
        initListeners();
        initData();
    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        viewScreen = findViewById(R.id.viewScreen);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                QuestionDetailActivity.this.finish();
            }
        });

        //筛选
        viewScreen.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(final View v) {
                if (Mutils.isNetworkAvailable()) {
                    final StudentScreenDialog dialog = new StudentScreenDialog(QuestionDetailActivity.this, R.style.Dialog_NoTitle);
                    dialog.show();
                    dialog.setScreenListener(new StudentScreenDialog.IScreen() {
                        @Override
                        public void getScreenMode(int mode, int college_id, String college_name, int rank_id) {
                            EventBus.getDefault().post(new StudentRefreshEvent(String.valueOf(college_id),college_name,String.valueOf(rank_id)));
                            dialog.dismiss();
                        }
                    });
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    //初始化数据
    private void initData(){
        tabList.add("实战真题");
        tabList.add("模拟训练");
        fragmentList.add(StudentShiZhanFragment.newInstance(student_id,student_avatar,student_nickname));
        fragmentList.add(StudentMoNiFragment.newInstance(student_id,student_avatar,student_nickname));

        tabAdapter = new TabAdapter(fragmentList, tabList,getSupportFragmentManager(), this);
        viewPager.setAdapter(tabAdapter);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(tabAdapter.getTabView(i));
            }

            if(i==0){
                //设置默认进来第一个是选中状态
                TabLayout.Tab a = tabLayout.getTabAt(0);
                View view1 = a.getCustomView();
                if (null != view1 && view1 instanceof TextView) {
                    ((TextView) view1).setTextSize(16);
                    ((TextView) view1).setTextColor(getResources().getColor(R.color.color_33));
                }
            }else{
                //设置默认进来第二个是不选中状态
                TabLayout.Tab b = tabLayout.getTabAt(1);
                View view2 = b.getCustomView();
                if (null != view2 && view2 instanceof TextView) {
                    ((TextView) view2).setTextSize(13);
                    ((TextView) view2).setTextColor(getResources().getColor(R.color.color_65));
                }
            }

        }

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //切换导航菜单时候的监听
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition(), true);
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView) view).setTextSize(16);
                    ((TextView) view).setTextColor(getResources().getColor(R.color.color_33));
                    ((TextView) view).setTextAppearance(QuestionDetailActivity.this, R.style.TabLayoutTextStyle);
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView) view).setTextSize(13);
                    ((TextView) view).setTextColor(getResources().getColor(R.color.color_65));
                }
            }


            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
